﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using WeiboPlus.Models;
using System.Threading;
using WeiboPlus.Service;
using WeiboPlus;
using Microsoft.Practices.Prism.Events;
using WeiboPlus.ViewModel;
using System.Collections.Generic;

namespace WeiboPlus.Shell.ViewModel
{
    public class SearchViewModel : CollectionVMBase<SNSStatusViewModel>
    {
        IStatusService _statusSvc;
        IUserService _userSvc;
        //private IEventAggregator _ev;
        int pagecount = 10;
        int pivotSelectedIndex;

        public int PivotSelectedIndex
        {
            get { return pivotSelectedIndex; }
            set
            {
                pivotSelectedIndex = value;
                RaisePropertyChanged("PivotSelectedIndex");
            }
        }

        string searchKeyword;

        public string SearchKeyword
        {
            get { return searchKeyword; }
            set
            {
                searchKeyword = value;

                //if (PivotSelectedIndex != 4)
                //{
                //    PivotSelectedIndex = 4;
                //}

                RaisePropertyChanged(() => SearchKeyword);
            }
        }

        ObservableCollection<SNSUser> users;

        public ObservableCollection<SNSUser> Users
        {
            get {
                if (users == null)
                    users = new ObservableCollection<SNSUser>();
                return users; }
            set
            {
                users = value;
                RaisePropertyChanged("Users");
            }
        }

        public SearchViewModel()
        {
            _userSvc = PhoneApplicationController.Current.Container.Resolve<IUserService>();
            _statusSvc = PhoneApplicationController.Current.Container.Resolve<IStatusService>();
        }

        protected override void Initialize()
        {
            //base.Initialize();
        }

        public void Search()
        {
            if (string.IsNullOrEmpty(SearchKeyword))
            {
                return;
            }

            Loading = true;
            var request = new SNSRequest() { Type = SNSType.SearchStatus };
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.PAGE, "1")); 
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.COUNT, pagecount+ ""));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.PAGE_FLAG, "1"));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.REQ_NUM, pagecount + ""));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.PAGE_INFO, ""));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.TREND_NAME, SearchKeyword));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.Q, SearchKeyword));

            //for tencent
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.PAGE_SIZE, pagecount + ""));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.KEY_WORD, SearchKeyword));
            request.Parameters.Add(new SNSParameter(SNSParametersConstants.CONTENTTYPE, 0+""));

            Items.Clear();
            Users.Clear();
            //_pagingInfo = null;
            _statusSvc.GetTimeline(
            request, (data, result) =>
            {
                if (!result.IsSuccess)
                {
                    ExecuteOnUIThread(() => { MessageBox.Show(result.Message); Loading = false; });
                    return;
                }
                //if (result.PagingInfo != null)
                //{
                //    _pagingInfo = result.PagingInfo;
                //}
                PopupItems<SNSStatusViewModel, SNSStatus>(Items, data, false, () =>
                {
                    //if (OnNextPagePerformed != null)
                    //    OnNextPagePerformed(SearchRsults.FirstOrDefault(), "search");
                });

            });

            var requestU = new SNSRequest() { Type = SNSType.SearchUser };
            requestU.Parameters.Add(new SNSParameter(SNSParametersConstants.PAGE, "1"));
            requestU.Parameters.Add(new SNSParameter(SNSParametersConstants.COUNT, pagecount + ""));
            requestU.Parameters.Add(new SNSParameter(SNSParametersConstants.Q, SearchKeyword));
            //for tencent
            requestU.Parameters.Add(new SNSParameter(SNSParametersConstants.PAGE_SIZE, pagecount + ""));
            requestU.Parameters.Add(new SNSParameter(SNSParametersConstants.KEY_WORD, SearchKeyword));
            //request.Type = SNSType.SearchUser;
            _userSvc.SearchUser(
            requestU, (data, result) =>
            {
                if (!result.IsSuccess)
                {
                    ExecuteOnUIThread(() => { MessageBox.Show(result.Message); Loading = false; });
                    return;
                }
                //if (result.PagingInfo != null)
                //{
                //    _pagingInfo = result.PagingInfo;
                //}
                PopupUsers(Users, data);
            });
        }

        private void PopupUsers(ObservableCollection<SNSUser> items, IList<SNSUser> data)
        {
            foreach (var i in data)
            {
                ExecuteOnUIThread<SNSUser>((user) =>
                {
                    items.Add(user);
                }, i);
                Thread.Sleep(80);
            }
            Thread.Sleep(400);
            ExecuteOnUIThread(() => { Loading = false; });
        }

        private void SearchTopics(string word)
        {
 
        }

        private void SearchUsers(string word)
        {

        }

        public void SuggestionStatuses(string keyWord)
        {
 
        }
    }
}
